﻿using System;
using System.Configuration;
using System.Diagnostics;
using TestDriver.Client.CatalogueServiceReference;
using TestDriver.Client.CustomerServiceReference;
using TestDriver.Client.OrderServiceReference;
using TestDriver.Client.StockServiceReference;
using TestDriver.Client.StoreServiceReference;
using TestDriver.Client.WarehouseServiceReference;
using VideoStore.Common.Enum;

namespace TestDriver.Client
{
    static class Program
    {
        private static readonly Random random = new Random();
        private static Process videoStoreProcess;
        private static Process warehouseProcess;

        static void Main(string[] args)
        {
            try
            {
                //StartUpHosts();

                // **********************
                // ** CUSTOMER SERVICE **
                // **********************
                var customerProxy = new CustomerServiceClient();
                customerProxy.Open();
                CustomerDto customerDto = new CustomerDto();
                int r = GetRandomNumber();
                customerDto.Name = "Cust " + r;
                customerDto.Address = "Address " + r;
                var customerCreateResponse = customerProxy.CreateCustomer(customerDto);
                customerProxy.Close();
                Console.WriteLine("Created customer id: {0}", customerCreateResponse.ResponseObject.Id);

                // ***********************
                // ** CATALOGUE SERVICE **
                // ***********************
                var catalogueProxy = new CatalogueServiceClient();
                catalogueProxy.Open();
                CatalogueDto video1 = new CatalogueDto();
                r = GetRandomNumber();
                video1.CatNo = r.ToString();
                video1.Director = "Director " + r;
                video1.Genre = "Genre " + r;
                video1.Price = 10.17F;
                video1.Title = "Title " + r;
                var catalogueCreateResponse1 = catalogueProxy.CreateVideo(video1);
                Console.WriteLine("Created video id: {0}", catalogueCreateResponse1.ResponseObject.Id);
                CatalogueDto video2 = new CatalogueDto();
                r = GetRandomNumber();
                video2.CatNo = r.ToString();
                video2.Director = "Director " + r;
                video2.Genre = "Genre " + r;
                video2.Price = 22.21F;
                video2.Title = "Title " + r;
                var catalogueCreateResponse2 = catalogueProxy.CreateVideo(video2);
                catalogueProxy.Close();
                Console.WriteLine("Created video id: {0}", catalogueCreateResponse2.ResponseObject.Id);

                // ***********************
                // ** WAREHOUSE SERVICE **
                // ***********************
                var warehouseProxy = new WarehouseServiceClient();
                warehouseProxy.Open();
                WarehouseDto warehouseDto = new WarehouseDto();
                r = GetRandomNumber();
                warehouseDto.Name = "Warehouse " + r;
                var warehouseCreateResponse1 = warehouseProxy.CreateWarehouse(warehouseDto);
                Console.WriteLine("Created warehouse id: {0}", warehouseCreateResponse1.ResponseObject.Id);
                warehouseDto = new WarehouseDto();
                r = GetRandomNumber();
                warehouseDto.Name = "Warehouse " + r;
                var warehouseCreateResponse2 = warehouseProxy.CreateWarehouse(warehouseDto);
                Console.WriteLine("Created warehouse id: {0}", warehouseCreateResponse2.ResponseObject.Id);
                warehouseProxy.Close();


                // *******************
                // ** STOCK SERVICE **
                // *******************
                var stockProxy = new StockServiceClient();
                stockProxy.Open();
                StockDto item = new StockDto();
                item.CatalogueId = catalogueCreateResponse1.ResponseObject.Id;
                item.Holding = 15;
                item.WarehouseId = warehouseCreateResponse1.ResponseObject.Id;
                var itemCreateResponse1 = stockProxy.CreateItem(item);
                Console.WriteLine("Created item id: {0}", itemCreateResponse1.ResponseObject.Id);
                item = new StockDto();
                item.CatalogueId = catalogueCreateResponse2.ResponseObject.Id;
                item.Holding = 10;
                item.WarehouseId = warehouseCreateResponse2.ResponseObject.Id;
                var itemCreateResponse2 = stockProxy.CreateItem(item);
                Console.WriteLine("Created item id: {0}", itemCreateResponse2.ResponseObject.Id);
                item = new StockDto();
                item.CatalogueId = catalogueCreateResponse1.ResponseObject.Id;
                item.Holding = 5;
                item.WarehouseId = warehouseCreateResponse2.ResponseObject.Id;
                var itemCreateResponse3 = stockProxy.CreateItem(item);
                Console.WriteLine("Created item id: {0}", itemCreateResponse3.ResponseObject.Id);
                stockProxy.Close();

                // *******************
                // ** STORE SERVICE **
                // *******************
                var storeProxy = new StoreServiceClient();
                storeProxy.Open();
                StoreDto storeDto = new StoreDto();
                r = GetRandomNumber();
                storeDto.Name = "Store " + r;
                var storeCreateResponse = storeProxy.CreateStore(storeDto);
                storeProxy.Close();
                Console.WriteLine("Created store id: {0}", storeCreateResponse.ResponseObject.Id);

                // *******************
                // ** ORDER SERVICE **
                // *******************
                var orderProxy = new OrderServiceClient();
                orderProxy.Open();
                OrderDto orderDto  = new OrderDto();
                Guid orderId = Guid.NewGuid();
                orderDto.Id = orderId;
                orderDto.OrderDate = DateTime.Now;
                orderDto.OrderStatus = OrderStatus.Submitted;
                orderDto.CustomerId = customerCreateResponse.ResponseObject.Id;
                orderDto.StoreId = storeCreateResponse.ResponseObject.Id;
                orderDto.WarehouseId = warehouseCreateResponse1.ResponseObject.Id;
                orderDto.OrderLinesDto = new OrderLinesDto[2];
                orderDto.OrderLinesDto[0] = new OrderLinesDto();
                orderDto.OrderLinesDto[0].OrderId = orderId;
                orderDto.OrderLinesDto[0].CatalogueId = catalogueCreateResponse1.ResponseObject.Id;
                orderDto.OrderLinesDto[0].Quantity = 1;
                orderDto.OrderLinesDto[1] = new OrderLinesDto();
                orderDto.OrderLinesDto[1].OrderId = orderId;
                orderDto.OrderLinesDto[1].CatalogueId = catalogueCreateResponse2.ResponseObject.Id;
                orderDto.OrderLinesDto[1].Quantity = 5;
                var orderCreateResponse = orderProxy.CreateOrder(orderDto);
                Console.WriteLine("Created order id: {0}", orderCreateResponse.ResponseObject.Id);
                var orderSubmitResponse = orderProxy.SubmitOrder(orderCreateResponse.ResponseObject.Id);
                Console.WriteLine("Submitted order id: {0}", orderCreateResponse.ResponseObject.Id);
                orderProxy.Close();

                Console.WriteLine();
                Console.WriteLine("All services have been invoked. This is the end of the Test Driver.");
                Console.ReadKey();
                ShutDownHosts();
            }
            catch (Exception ex)
            {
                ShutDownHosts();
                Console.WriteLine("An error occured: {0}", ex.Message);
                Console.ReadKey();
            }
        }

        private static void ShutDownHosts()
        {
            if (videoStoreProcess != null && !videoStoreProcess.HasExited)
            {
                videoStoreProcess.Kill();
            }
            if (warehouseProcess != null && !warehouseProcess.HasExited)
            {
                warehouseProcess.Kill();
            }
        }

        private static void StartUpHosts()
        {
            string videoStoreHost = ConfigurationManager.AppSettings["videoStoreHostPath"];
            string warehouseHost = ConfigurationManager.AppSettings["warehouseHostPath"];
            videoStoreProcess = Process.Start(videoStoreHost);
            warehouseProcess = Process.Start(warehouseHost);
        }

        private static int GetRandomNumber()
        {
            int r = random.Next(1000);
            return r;
        }
    }
}